using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Text;
using UnityEngine;
using UnityEngine.SceneManagement;

public class Login : MonoBehaviour
{
    string user = "";
    string pwd = "";
    /// <summary>
    /// 只被调用一次,变量声明
    /// </summary>
    void Awake()
    {
        //NetWriter.SetMd5Key("");
    }

    /// <summary>
    /// 只被调用一次
    /// </summary>
    void Start()
    {
        ////5.0版本打包时候选中需要打包的东西然后设置右下角名称,同个/设置多集目录,后面的框标记后缀(后缀不重要)  
        ////打包时候的目标文件夹,假设目标文件夹名称为"WJJ",那么会生成"WJJ"和"WJJ.manifest"两个文件  
        ////其中WJJ.manifest文件没有用,只是用来看的,WJJ是一个assetbundle包,里面包含了整个文件夹的依赖信息  
        ////可以先加载这个东西,然后获取到依赖关系后逐步加载  

        //string savePath = Application.persistentDataPath;
        ////ShowTip.Instance.ShowMessage(savePath);

        //try
        //{
        //    LoadAssetBundle.Instance.DownLoadAssets2LocalWithDependencies("http://192.168.1.200:8001/Upload/Assets", "Windows", "Windows", savePath, () =>
        //    {
        //        GameObject obj = LoadAssetBundle.Instance.GetLoadAssetFromLocalFile("Windows", "Windows", "Windows", Application.persistentDataPath);
        //        GameObject.Instantiate(obj);
        //        //ShowTip.Instance.ShowMessage(obj.name);
        //        //obj.GetComponent<Renderer>().sharedMaterial.shader = Shader.Find(obj.GetComponent<Renderer>().sharedMaterial.shader.name);  
        //    });

        //}
        //catch (Exception ex)
        //{
        //    Debug.Log(ex.Message);
        //}

        //NetWriter.SetUrl("127.0.0.1:9001");
    }

    /// <summary>
    /// 每一帧调用
    /// </summary>
    void Update()
    {

    }

    /// <summary>
    /// 每一帧调用,处理Rigdibody时代替Update方法
    /// </summary>
    void FixedUpdate()
    {

    }

    /// <summary>
    /// 每一帧调用,Update方法全部执行完后执行
    /// </summary>
    void LateUpdate()
    {

    }

    /// <summary>
    /// 每一帧被调用多次,enabled=false时禁用
    /// </summary>
    void OnGUI()
    {
        NetWriter.SetMd5Key("qqqqqq");
        int cwidth = Screen.width / 2;
        int cheight = Screen.height / 2;
        var boxPos = new Rect(cwidth - 110, cheight - 160, 240, 120);

        GUI.Box(boxPos, "");
        GUI.Label(new Rect(cwidth - 100, cheight - 150, 100, 22), "User:");
        user = GUI.TextField(new Rect(cwidth - 10, cheight - 150, 120, 22), user, 20);
        GUI.Label(new Rect(cwidth - 100, cheight - 120, 100, 22), "Password:");
        pwd = GUI.PasswordField(new Rect(cwidth - 10, cheight - 120, 120, 22), pwd, '*', 20);

        if (GUI.Button(new Rect(cwidth - 100, cheight - 80, 80, 22), "Regist"))
        {
            ////正常注册用户
            //GameSetting.Instance.Pid = user;
            //GameSetting.Instance.Password = pwd;
            //NetWriter.SetUrl("http://192.168.1.200:8002/Default.ashx", ResponseContentType.Json, true);
            //ActionParam ap = new ActionParam();
            //ap["Handler"] = "Regist";
            //Net.Instance.Send(1000, onLogin, ap);

            //游客注册
            NetWriter.SetUrl("http://www.aladdin.accountserver/Default.ashx", ResponseContentType.Json, true);
            ActionParam ap = new ActionParam();
            ap["Handler"] = "Passport";
            Net.Instance.Send(1000, onPassport, ap);
        }

        if (GUI.Button(new Rect(cwidth, cheight - 80, 80, 22), "Login"))
        {
            //登陆账号服务获取Token
            GameSetting.Instance.Pid = user;
            GameSetting.Instance.Password = pwd;
            NetWriter.SetUrl("http://www.aladdin.accountserver/Default.ashx", ResponseContentType.Json, true);
            ActionParam ap = new ActionParam();
            ap["Handler"] = "Login";
            Net.Instance.Send(1000, onLogin, ap);

        }

    }

    private void onLogin(ActionResult obj)
    {
        NetWriter.SetUrl("127.0.0.1:9001");
        ActionParam ap = new ActionParam();
        ap["Token"] = obj.Get<string>("Token");
        Net.Instance.Send((int)ActionType.Login, LoginCallback, ap);
    }

    private void onPassport(ActionResult obj)
    {
        user = obj.Get<string>("PassportId");
        pwd = obj.Get<string>("Password");

        GameSetting.Instance.Pid = user;
        GameSetting.Instance.Password = pwd;
    }

    public class RData
    {
        public int StateCode { get; set; }

        public string StateDescription { get; set; }

        public string Vesion { get; set; }

        public string Handler { get; set; }

        public Data Data { get; set; }
    }

    public class Data
    {
        public string Token { get; set; }

        public string UserID { get; set; }

        public string PassportId { get; set; }

        public string Password { get; set; }

    }

    private void RegistCallback(ActionResult actionResult)
    {
        if (actionResult != null)
        {
            user = actionResult.Get<string>("passportID");
            pwd = actionResult.Get<string>("password");
        }
    }

    private void LoginCallback(ActionResult actionResult)
    {
        Debug.Log("AAAAAAAAAAAAAAAAuserId=" + NetWriter.SessionID);
        if (actionResult != null && actionResult.Get<int>("GuideID") == (int)ActionType.CreateRote)
        {
            //Application.LoadLevelAsync("RoleScene");
            SceneManager.LoadSceneAsync("RoleScene");
            return;
        }

        //ActionParam ap = new ActionParam();
        //ap["PageIndex"] = 1;
        //ap["PageSize"] = 20;
        //Net.Instance.Send(1001, OnRankCallback, ap);

        //Application.LoadLevelAsync("MainScene");
        SceneManager.LoadSceneAsync("MainScene");
    }

    private void OnRankCallback(ActionResult actionResult)
    {
        if (actionResult != null)
        {
            //string userName = actionResult.Get<string>("UserName");
            //int score = actionResult.Get<int>("Score");
            //var lst = actionResult.Get<List<FriendsServerData>>("list");
            Response1001Pack pack = actionResult.GetValue<Response1001Pack>();
            if (pack == null)
            {
                return;
            }
            var lst = pack.Items;
            string a = "";
        }
    }

    /// <summary>
    /// 不销毁对象,场景切换时对象依然存在.
    /// </summary>
    void DontDestroyOnLoad()
    {

    }
}

public class FriendsServerData
{
    public string UserName;
    public int Score;
}
